import axios from 'axios'
const config = require('./config') 
axios.defaults.timeout = 50000;  
import router from '../router'

import { Message } from 'element-ui';
//返回状态判断(添加响应拦截器)
axios.interceptors.response.use((res) =>{
    console.log(res.data)
    if(res.data.code&&res.data.code==999||res.data.code==996){
        router.replace('/')
    }
    if(res.data.code &&res.data.code!= 200){
        Message.error(res.data.message);
    }
    return res;
}, (error) => {
    return Promise.reject(error);
});
axios.interceptors.request.use(
    config => {
        const token = localStorage.getItem("shopPC");
        token && (config.headers.mlfastsessionid = token);
        return config;
    },
    error => {
        return Promise.error(error);
    }
);
//返回一个Promise(发送post请求)
export function fetchPost(url, params={}) {
    return axios({
        method:"POST",
        url:url,
        data:params,
    })
};
export function fetchPost2(url, params={}) {
    return axios({
        method:"POST",
        url:url,
        data:params,
    })
};
export function fetchPost3(url, params={}) {
    return axios({
        method:"POST",
        url:url,
        data:params,
        responseType: 'blob'
    })
};
////返回一个Promise(发送get请求)
export function fetchGet(url, param) {
    return new Promise((resolve, reject) => {
        axios.get(url, {params: param})
            .then(response => {
                resolve(response)
            }, err => {
                reject(err)
            })
            .catch((error) => {
                reject(error)
            })
    })
}
export default {
    fetchPost,
    fetchGet,
    fetchPost2,
    fetchPost3
}